package com.tutu.model;

import android.content.Context;

import com.tutu.model.bean.UserInfo;
import com.tutu.model.dao.UserAccountDao;
import com.tutu.model.db.DBManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 图图 2020/10/9
 *
 * 数据模型层全局类
 */
public class Model {

    private  Context mContext;

    // 创建对象
    private static Model model = new Model();

    private DBManager dbManager;

    // 定义线程池
    private ExecutorService executors = Executors.newCachedThreadPool();

    // 用户账号数据库操作对象
    private UserAccountDao userAccountDao;

    // 私有构造
    private Model(){}

    // 获取单例对象
    public static Model getInstance(){
        return model;
    }

    // 初始化方法
    public  void  init(Context context){
        mContext = context;

        // 创建用户账号数据库操作对象
        userAccountDao = new UserAccountDao(context);

        // 创建全局监听
        EventListener eventListener = new EventListener(context);
    }

    // 获取全局线程池对象
    public  ExecutorService getGlobalThreadPool(){
        return  executors;
    }

    /**
     * 用户登录成功后的处理方法
     */
    public void loginSuccess(UserInfo account) {
        if(account == null) return;
        if(dbManager != null) dbManager.close();
        dbManager = new DBManager(mContext, account.getUsername());
    }

    /**
     * 获取用户账号操作数据库的对象
     * @return
     */
    public UserAccountDao getUserAccountDao(){
        return  userAccountDao;
    }

    public DBManager getDbManager(){
        return dbManager;
    }
}
